package quanlyhochieu.timvitriluutru;
import java.util.*;

import utils.DateUtils;

import dbutil.ConnectDB;
public class TimViTriLuuTru {
	private String error;
	private String soHC;
	private List<HoChieu> listHC;
	
	public TimViTriLuuTru(){}

	public String getError() {
		return error;
	}

	public void setError(String error) {
		this.error = error;
	}

	public String getSoHC() {
		return soHC;
	}

	public void setSoHC(String soHC) {
		this.soHC = soHC;
	}



	public void setListHC(List<HoChieu> listHC) {
		this.listHC = listHC;
	}
	int i =0;
	public List<HoChieu> getListHC() {
		if(i ==1)this.displayAll();
		else this.timTheoMa();
		return this.listHC;
		
	}
	public String timTheoMa(){
		String result = "timvitriluutru";
		i =0;
		this.setError("");
		List<HoChieu> list = new ArrayList<HoChieu>();
		String SQL ="";
		if(this.soHC != null){
			SQL ="select * from hochieu as HC, canhan as CN, vitriluutru as VTLT  where CN.cmnd = HC.cmnd and HC.sohc = VTLT.sohc and HC.sohc like '%"+this.soHC+"%'";
		}
		else SQL = "select * from hochieu as HC,canhan as CN, vitriluutru as VTLT  where CN.cmnd = HC.cmnd and HC.sohc = VTLT.sohc";
		System.out.println("SQL "+SQL);
		ArrayList<HashMap<String, Object>> array = ConnectDB.getDataTypeList(SQL);
		if (array.size() > 0) {
			HashMap<String, Object> hash = new HashMap<String, Object>();
			int stt = 0;
			for (int i = 0; i < array.size(); i++) {
				stt = i + 1;
				hash = (HashMap<String, Object>) array.get(i);
				HoChieu HC = new HoChieu();
				HC.setStt(stt);
				HC.setSoHC(hash.get("sohc").toString());
				HC.setLoaiHC(hash.get("loaihc").toString());
				String thoiHan = DateUtils.formatDate(hash.get("ngaycap").toString())+"-"+DateUtils.formatDate(hash.get("ngayhethan").toString());
				HC.setThoiHan(thoiHan);
				HC.setNoiCap(hash.get("noicap").toString());
				HC.setTen(hash.get("ten").toString());
				String viTriTu = hash.get("vitritu").toString();
				String nganTrongTu = hash.get("ngantrongtu").toString();
				String trangThai =hash.get("trangthainhan").toString();
				HC.setViTriLuu("Ngăn "+nganTrongTu+"_"+viTriTu);
				if(trangThai.equals("G")) HC.setTrangThai("Đang giao");
				else HC.setTrangThai("Đang nhận");
				
				
				list.add(HC);
			}
		}
		else this.setError("Không tìm thấy hộ chiếu có mã số đã nhập");
		this.setListHC(list);
		return result;
	}
	
	public String displayAll(){
		i =1;
		this.soHC =null;
		this.timTheoMa();
		return "timvitriluutru";
	}
	 

}
